﻿using System.Collections.Generic;
using UnityEngine;
using YooAsset;

namespace Client.Base
{
    public class PatchHandle : StateMachine
    {
        public string RemoteVersion;
        public EPlayMode playMode;
        public static PatchHandle Instance;
        private Dictionary<string, string> packVersions = new();
        private List<ResourceDownloaderOperation> downloaderOperations = new();
        private long totalDownloadBytes = 0;
        private long currentDownloadBytes;
        private Dictionary<string, string> packageVersions = new();
        private UIPatch uiPacth;

        public override void Run(object arg = null)
        {
            Instance = this;
            playMode = (EPlayMode)arg;
            this.Log($" [Patch]  playMode : {playMode}");

            AddState<FsmInitPatch>();
            // AddState<FsmNetConfig>();
            AddState<FsmInitPackage>();
            AddState<FsmRequestPackageVersion>();
            AddState<FsmUpdatePackageManifest>();
            AddState<FsmCreateDownloader>();
            AddState<FsmDownloadPackageFiles>();
            AddState<FsmDownloadPackageOver>();
            AddState<FsmClearUnUsedAssets>();
            AddState<FsmStartGame>();
            AddState<FsmExitPatch>();

            Start();
        }

        private void Start()
        {
            Change<FsmInitPatch>();
        }

        public override void Release()
        {
            uiPacth.Release();
            base.Release();
            Instance = null;
        }

        public void SetUIPatch(UIPatch ui)
        {
            this.uiPacth = ui;
        }

        public void ShowProgressMessage(string message)
        {
            uiPacth.ShowProgressMessage(message);
        }

        public void ShowErrorMessage(string message)
        {
            uiPacth.ShowErrorMessage(message);
        }

        public void SetProgress(float v)
        {
            uiPacth.SetProgress(v);
        }

        public string GetPatchURL(string packageName)
        {
            return $"http://127.0.0.1/res/{packageName}";
        }

        public string GetPackageVersion(string packageName)
        {
            if (packageVersions.TryGetValue(packageName, out var version))
            {
                return version;
            }
            return "1.0";
        }

        public void SetPackageVersion(string packageName, string packageVersion)
        {
            packageVersions.TryAdd(packageName, packageVersion);
        }

        public List<ResourceDownloaderOperation> GetDownloaders()
        {
            return downloaderOperations;
        }

        public void SetDownloaders(List<ResourceDownloaderOperation> mergeDownloaders)
        {
            downloaderOperations.Clear();
            downloaderOperations = mergeDownloaders;

            foreach (var item in downloaderOperations)
            {
                totalDownloadBytes += item.TotalDownloadBytes;
            }
        }

        public void DownloadErrorCallback(DownloadErrorData data)
        {

        }

        public void DownloadUpdateCallback(DownloadUpdateData data)
        {
            currentDownloadBytes += data.CurrentDownloadBytes;
        }
    }

}

